<?php

App::uses('AppController', 'Controller');

/**
 * ClientCompanies Controller
 *
 * @property ClientCompany $ClientCompany
 */
class ClientCompaniesController extends AppController {

  function beforeFilter() {
    $this->set('title_for_layout', 'Empresas');
  }

  /**
   * index method
   *
   * @return void
   */
  public function index() {
    $this->ClientCompany->recursive = 0;
    $this->set('clientCompanies', $this->paginate());
  }

  public function search() {
    $name = '%' . trim($this->request->data('name')) . '%';
    $this->paginate = array(
        'conditions' => array('ClientCompany.social_razon LIKE' => $name));
    $this->set('clientCompanies', $this->paginate());
    $this->layout = 'ajax';
  }

  /**
   * ajax index method
   *
   * @return void
   */
  public function indexAjax() {
    $this->ClientCompany->recursive = 0;
    $this->paginate = array('limit' => 1000);
    $this->set('clientCompanies', $this->paginate());
    $this->set('title_for_layout', 'Transporte Villa');
    $this->layout = 'ajax';
  }

  /**
   * searchCompanies ajax method 
   *
   * @return void
   */
  public function search2() {
    $name = '%' . trim($this->request->data('name')) . '%';
    $this->set('companies', $this->paginate(array('Company.social_razon LIKE' => $name)));
    $this->layout = 'ajax';
  }

  /**
   * view method
   *
   * @throws NotFoundException
   * @param string $id
   * @return void
   */
  public function view($id = null) {
    if (!$this->ClientCompany->exists($id)) {
      throw new NotFoundException(__('Invalid client company'));
    }
    $options = array('conditions' => array('ClientCompany.' . $this->ClientCompany->primaryKey => $id));
    $this->set('clientCompany', $this->ClientCompany->find('first', $options));
  }

  /**
   * add method
   *
   * @return void
   */
  public function add() {
    if ($this->request->is('post')) {
      $this->ClientCompany->create();
      if ($this->ClientCompany->save($this->request->data)) {
        $this->Session->setFlash(__('The client company has been saved'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('The client company could not be saved. Please, try again.'));
      }
    }
  }

  /**
   * edit method
   *
   * @throws NotFoundException
   * @param string $id
   * @return void
   */
  public function edit($id = null) {
    $this->ClientCompany->id = $id;
    if (!$this->ClientCompany->exists($id)) {
      throw new NotFoundException(__('Invalid client company'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
      if ($this->ClientCompany->save($this->request->data)) {
        $this->Session->setFlash(__('The client company has been saved'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('The client company could not be saved. Please, try again.'));
      }
    } else {
      $options = array('conditions' => array('ClientCompany.' . $this->ClientCompany->primaryKey => $id));
      $this->request->data = $this->ClientCompany->find('first', $options);
    }
  }

  /**
   * delete method
   *
   * @throws NotFoundException
   * @throws MethodNotAllowedException
   * @param string $id
   * @return void
   */
  public function delete($id = null) {
    $this->ClientCompany->id = $id;
    if (!$this->ClientCompany->exists()) {
      throw new NotFoundException(__('Invalid client company'));
    }
    $this->request->onlyAllow('post', 'delete');
    if ($this->ClientCompany->delete()) {
      $this->Session->setFlash(__('Client company deleted'));
      $this->redirect(array('action' => 'index'));
    }
    $this->Session->setFlash(__('Client company was not deleted'));
    $this->redirect(array('action' => 'index'));
  }

}
